﻿using System;
using System.Windows;
using System.Windows.Input;
using Silvery;
using Silvery.Role.Simple;
using Silvery.Loader;
namespace Sample02_Role
{
    public enum HeroState
    {
        None,站立,跑
    }
    public enum RoleDir
    {
        None,左,右
    }
    public class Sample_5 : GameRoot
    {
        protected SimpleRole_Hero hero = new SimpleRole_Hero();
        public Sample_5()
        {
            this.AddBaseObj(hero);
            App.Current.RootVisual.KeyDown += new KeyEventHandler(RootVisual_KeyDown);
            App.Current.RootVisual.KeyUp += new KeyEventHandler(RootVisual_KeyUp);
            hero.Position = new Point(200, 200);
            IsSelfLoopRun = true;
        }

        protected virtual void RootVisual_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.A || e.Key == Key.D)
                hero.HeroState = HeroState.站立;
            else
                if (e.Key == Key.J)
                    hero.IsAttack = false;
        }

        protected virtual void RootVisual_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.J)
                hero.IsAttack = true;
            if (e.Key == Key.A || e.Key == Key.D)
                if (hero.HeroState == HeroState.跑)
                    return;
                else
                    hero.HeroState = HeroState.跑;
            if (e.Key == Key.A)
                hero.RoleDir = RoleDir.左;
            else
                if (e.Key == Key.D)
                    hero.RoleDir = RoleDir.右;
        }
        double speed = 5;
        public override void OnUpdata(BaseObject sender, ITimer timer)
        {
            if (hero.HeroState == HeroState.跑)
            {
                if (hero.RoleDir == RoleDir.右)
                    hero.X += speed;
                else
                    if (hero.RoleDir == RoleDir.左)
                        hero.X -= speed;
            }
            base.OnUpdata(sender, timer);
        }
        
    }
    public class SimpleRole_Hero : SimpleRole
    {
        UpperBody upperbody = new UpperBody();
        public SimpleRole_Hero()
            : base(new Uri("download/Sample_Role/DownBody.zip", UriKind.Relative))
        {
            RoleName = "我是一个英雄";

            var upperspirit = new SpiritLoader(new Uri("download/Sample_Role/UpperBody01.zip", UriKind.Relative), upperbody);
            upperspirit.BeginDownLoad();
            this.Parts.Add("UpperBody", upperbody);
            base.BeginDownload();
        }

        protected override void OnBodyPackLoaded()
        {
            HeroState = Sample02_Role.HeroState.站立 ;
            base.OnBodyPackLoaded();
        }
        HeroState herostate;
        public HeroState HeroState
        {
            get { return herostate; }
            set
            {
                if (herostate == value)
                    return;
                herostate = value; CurrentAnimatName = value.ToString();
                IsAttack = IsAttack;
            }
        }
        RoleDir roledir;
        public RoleDir RoleDir
        {
            get { return roledir; }
            set
            {
                if (roledir == value)
                    return;
                roledir = value;
                if (value == Sample02_Role.RoleDir.右)
                    IsMirror = true;
                else
                    IsMirror = false;
                IsAttack = IsAttack;
            }
        }
        public new bool IsMirror
        {
            set
            {
                BodyPart.IsMirror = value;
                upperbody.IsMirror = value;
            }
        }
        private bool isAttack;

        public bool IsAttack
        {
            get { return isAttack; }
            set
            {
                isAttack = value;
                if (value)
                    upperbody.CurrentAnimatName = "攻击";
                else
                    upperbody.CurrentAnimatName = "站立";
            }
        }

        //上身组件
        public class UpperBody : NSpirit
        {
            public UpperBody()
            {
                Z = -1;
            }
            double offsety = 1;
            TimeSpan timespan = TimeSpan.FromMilliseconds(240);
            ITimer calltimer = new CallTimer();
            public override void OnUpdata(BaseObject sender, ITimer timer)
            {
                if (calltimer.LastCallTimeSpan > timespan)
                {
                    offsety = -offsety;
                    this.Position = new Point(Position.X, Position.Y + offsety);
                    calltimer.Call();
                }
                base.OnUpdata(sender, timer);
            }
        }
    }
}
